package com.feng.seata.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.feng.seata.model.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface ProductMapper extends BaseMapper<Product> {

    @Update("update product_info set stock = stock - #{buyNum}")
    void minusStock(@Param("buyNum") Integer buyNum);

    @Update("update product_info set stock = stock - #{buyNum}, pre_deduct_stock = pre_deduct_stock + #{buyNum} where id = #{productId} and stock >= #{buyNum}")
    int prepareDeductStock(@Param("productId") Long productId, @Param("buyNum") Integer buyNum);

    @Update("update product_info set pre_deduct_stock = pre_deduct_stock - #{buyNum} where id = #{productId} and pre_deduct_stock >= #{buyNum}")
    int commitDeductStock(@Param("productId") Long productId, @Param("buyNum") Integer buyNum);

    @Update("update product_info set stock = stock + #{buyNum}, pre_deduct_stock = pre_deduct_stock - #{buyNum} where id = #{productId} and pre_deduct_stock >= #{buyNum}")
    int rollbackDeductStock(@Param("productId") Long productId, @Param("buyNum") Integer buyNum);

}
